import request from "@/utils/request";

const BINLOG_BASE_URL = "/api/v1/binlog";

const BinLogAPI = {
  /** 获取分页数据 */
  getPage(queryParams?: BinLogPageQuery) {
    return request<any, PageResult<BinLogPageVO[]>>({
      url: `${BINLOG_BASE_URL}/page`,
      method: "get",
      params: queryParams,
    });
  },

  /** 获取下拉列表 */
  getOptions(dbId?: number) {
    return request<any, OptionType[]>({
      url: `${BINLOG_BASE_URL}/options/${dbId}`,
      method: "get",
    });
  },

  /**
   * 获取表单数据
   *
   * @param id 主键ID
   * @returns 表单数据
   */
  getFormData(id?: string) {
    return request<any, BinLogForm>({
      url: `${BINLOG_BASE_URL}/${id}/form`,
      method: "get",
    });
  },

  /** 添加数据 */
  create(data?: BinLogForm) {
    return request({
      url: `${BINLOG_BASE_URL}`,
      method: "post",
      data,
    });
  },

  /**
   * 更新表单
   *
   * @param id 主键ID
   * @param data 表单数据
   */
  update(id?: string, data?: BinLogForm) {
    return request({
      url: `${BINLOG_BASE_URL}/${id}`,
      method: "put",
      data,
    });
  },

  /**
   * 批量删除，多个以英文逗号(,)分割
   *
   * @param ids 主键ID字符串，多个以英文逗号(,)分割
   */
  deleteByIds(ids?: string) {
    return request({
      url: `${BINLOG_BASE_URL}/${ids}`,
      method: "delete",
    });
  },

  /**
   * 更新BinLog列表
   *
   * @param id 数据源主键ID
   */
  syncBinLog(sourceId?: number) {
    return request({
      url: `${BINLOG_BASE_URL}/sync/${sourceId}`,
      method: "get",
    });
  },

  /**
   * 解析BinLog文件
   *
   * @param id 主键ID
   */
  handleParse(binLogId?: number) {
    return request({
      url: `${BINLOG_BASE_URL}/parse/${binLogId}`,
      method: "get",
    });
  },

  /**
   * 重置BinLog文件，上次解析位置
   *
   * @param id 主键ID
   */
  resetPosition(binLogId?: number) {
    return request({
      url: `${BINLOG_BASE_URL}/reset/${binLogId}`,
      method: "get",
    });
  },
};

export default BinLogAPI;

/** 分页查询参数 */
export interface BinLogPageQuery extends PageQuery {
  /** 数据源ID */
  dbId?: number;
  status?: number;
  createTime?: Date;
}

/** 分页对象 */
export interface BinLogPageVO {
  id?: string;
  dbId?: string;
  dbName?: string;
  dbNameAlias?: string;
  fileName?: string;
  fileSize?: number;
  position?: number;
  status?: number;
  prevPosition?: number;
  latestPosition?: number;
  createTime?: Date;
  updateTime?: Date;
  iSqlTime?: number;
  iSqlSaveTime?: number;
  iSqlSpeed?: number;
  iSqlCount?: number;
  rSqlTime?: number;
  rSqlSaveTime?: number;
  rSqlSpeed?: number;
  rSqlCount?: number;
  avgSqlTime?: number;
  avgSaveTime?: number;
  avgSpeed?: number;
  totalCount?: number;
}

/** 表单对象 */
export interface BinLogForm {
  id?: string;
  dbId?: string;
  dbName?: string;
  dbNameAlias?: string;
  fileName?: string;
  fileSize?: number;
  position?: number;
  status?: number;
}
